package wk10p3;

/*
 * This program will crash as is.
 *  It is meant to show that the expression "new T[ length ]" 
 *     will not compile
 *  
 *  One alternative would be to use
 *     (T[]) new Object[ length ]
 *    but this goes against two very important reasons for using
 *    Generics in the first place, which is to have type checking
 *    when compiling and to avoid cast operations.
 *    
 *  The actual "solution" to the problems is to use the the
 *   java.util classes.
 *   
 */
class Array<T> {
	private T[] x;
	public Array() {}
	public Array(T[] x_in) {
		if (x_in != null){
			int length = x_in.length;
//			x = new T[ length ];
			for (int i=0; i<x_in.length; i++) x[i] = x_in[i];
		}
	}
	public T[] getX() {	return x;	}
	public String toString() {
		String result = "[ ";
		for (T i : x)
			result = result + i + ", ";
		return  result + " ]";	
	}
}

public class GenericArraysOfThings {
	public static void main(String[] args) {
		Integer[] ix =  {7, 23, 102, -65};
		Double[] dx = {7.45, 0.23e-4, -34.25};
		String[] sx = {"Hello", "CSIS 235", "This is a", "good day"};
		
		Array<Integer> i = new Array<Integer>( ix );
		System.out.println(" i: " + i);
		Array<Double> d = new Array<Double>( dx );
		System.out.println(" d: " + d);
		Array<String> s = new Array<String>( sx );
		System.out.println(" s: " + s);
	}
}
